/**
 * AIT642-Software Testing & Maintenance

 * @author Ruchi Garg
 * @purpose
 * This is the first project, the purpose of this project is to 
 * change the program in Java language form C language. I have taken
 * extensive help from Google in order to write this program, as I am not
 * familiar with Java Programming.
 */
package task4;

import java.io.*;



// TODO: Auto-genrated Javadoc
/**
 * The Class Stutter, includes various methods to perform the purpose of the project.
 */
public class Stutter {	
	
	/**
	 * The main method. 
	 * The method stut included in the class Stutter will scan all the words character by character,
	 * in order to detect the repeated words.
	 * @param args the arguments is the input file to be read.
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader in;	// source file 
		Stutter stutter = new Stutter();
		
		if (args.length == 0) {
			// read input data.
			in = new BufferedReader(new InputStreamReader(System.in));			
			stutter.stut(in);
		} else {			
			for (int i = 0; i < args.length; i++) {
				try {					
					in = new BufferedReader(new FileReader(args[i]));
					stutter.stut(in);
					in.close();
				} catch (Exception e) {					
					System.out.println("stutter error: " + e.toString());
				}
			}

		}

	}

	

	/**
	 * <p><b>Algorithm:</b></p>
	 * <pre>
	 * while not the end of the source file {
	 * read a character
	 * if the character is a line feed,
	 * increase the line number
	 * if the character is a special delimiter
	 * if it's not at the beginning of current word {
	 * then it is the end of current word
	 * compare current word with previous word
	 * if they are the same
	 * print out
	 * else
	 * copy it to previous word
	 * }
	 * else
	 * append the character to current word
	 * } end of while
	 * </pre>.	 *
	 * @param in the input source
	 */
	public void stut(BufferedReader in) {
		char[] currentWord = new char[100];
		char[] previousWord = new char[100];
		int lineNo = 1;	// current line number
		boolean isBeginOfWord = true;

		int c = 0;		// the character read from the source
		int i = 0;		// the indicator of the character in the word
		previousWord[0] = '\0';		//initialize the previous word to empty


		// while not the end of file
		while (c != -1) {
			// This will read a single character
			try {
				c = in.read();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// if c is a line feed, increase the line number
			if ((char) c == '\n')
				lineNo++;
			
			// if c is a special char
			if (isSpecialChar((char) c)) {
				// if the special char is not at the beginning of the word
				if (!isBeginOfWord) {
					currentWord[i] = '\0';	// set the end of the word
					
					if (isSame(previousWord, currentWord))
						System.out.printf("Repeated word on line %d: %s %s\n",
								lineNo, arrayToString(previousWord),
								arrayToString(currentWord));
					else
						copyWord(currentWord, previousWord);					
					isBeginOfWord = true;
					i = 0;
				}
			} else {
				// c is not a special char, then append c to current word
				isBeginOfWord = false;
				currentWord[i++] = (char) c;
			}
		}
		System.out.println();
	}
	
	
	/**
	 * Checks if is special char.
	 *
	 * @param c the c
	 * @return true, if is special char
	 */
	private boolean isSpecialChar(char c) {
		final char[] DELIMITERS = { '\0', '\n', '\r', '\t', '	', ' ', ',', '.', '!', '-',
				'+', '=', ';', ':', '?', '&', '{', '}', '\\' };
		int nDELIMITERS = DELIMITERS.length;

		for (int i = 0; i < nDELIMITERS; i++)
			if (c == DELIMITERS[i])
				return true;

		return false;
	}
	
	
	/**
	 * Checks if is same.
	 * This method returns true if the compared words are same.
	 * The end of word is specified by \0 in the array.
	 * If all consecutive pairs of characters are same, the words are recognized as same,
	 * and if they have same length of characters.
	 * @param a the first word
	 * @param b the second word that follows first word which needs to be compared.
	 * @return true, if both are same
	 */
	private boolean isSame(char[] a, char[] b) {
		int i = 0;		
		while ((a[i] != '\0') && (b[i] != '\0')) {			
			if (a[i] != b[i])
				return false;			
			i++;	
		}			
		if ((a[i] == '\0') && (b[i] == '\0'))
			return true;
		else
			return false;

	}
	
	/**
	 * This copyWord method will function same as cp (a,b) procedure provided in
	 * source stutter.c file.  The word cp and parameters 'a' and 'b' were not
	 * intuitive enough for users, so changed them to copyWord, source and target.
	 * 
	 * @param source is the original or the source word which needs to be copied.
	 * @param target is the target location of word to be copied.
	 */
	private void copyWord(char[] source, char[] target) {
		int i = 0;		
		while (source[i] != '\0') {
			target[i] = source[i];
			i++;
		}
		target[i] = '\0';	// mark the end of word
	}
	
	/**
	 * Array to string. this method returns the string of the size corresponding
	 * to the size of the array.	 *
	 * @param source the source
	 * @return the string
	 */
	private String arrayToString(char[] source) {
		int i = 0;	
		//This will calculate the length of the word.
		while (source[i] != '\0') 
			i++;		
		char[] target = new char[i];		
		for (int j = 0; j < i; j++)
			target[j] = source[j];

		return new String(target);
	}

}